package com.sky.moduels.mini.wx.mp.config;

import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description: 公众号配置类
 * @ClassName: WxMpServiceConfig
 * @Author: hupengming
 * @CreateTime: 2022-08-19 11:41
 */
@Slf4j
@Configuration
public class WxMpServiceConfig {

    @Autowired
    WxMpConfig wxMpConfig;

    @Bean
    public WxMpConfigStorage getWxMpConfigStorage(){
        WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl();
        config.setToken(wxMpConfig.getToken());
        config.setAppId(wxMpConfig.getAppId());
        config.setSecret(wxMpConfig.getSecret());
        return config;
    }

    @Bean
    public WxMpService getWxMpService() throws WxErrorException {
        WxMpServiceImpl wxMpService = new WxMpServiceImpl();
        WxMpConfigStorage wxMpConfigStorage = this.getWxMpConfigStorage();
        log.info("加载的token:{}",wxMpConfigStorage.getToken());
        wxMpService.setWxMpConfigStorage(wxMpConfigStorage);
        return wxMpService;
    }

}
